package com.bobo.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.bobo.base.R;
import com.bobo.pojo.Basket;
import com.bobo.pojo.dto.ShopCartItemDto;
import com.bobo.service.BasketService;
import com.bobo.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/p/shopCart")
@Tag(name = "购物车接口")
public class ShopCartController {
    @Resource
    private BasketService basketService;
    @GetMapping("/prodCount")
    @Operation(summary = "获取购物车商品数量", description = "获取所有购物车商品数量")
    public R<Integer> prodCount() {
        String userId = SecurityUtils.getUser().getUserId();
        List<ShopCartItemDto> shopCartItems = basketService.getShopCartItems(userId);
        if (CollectionUtil.isEmpty(shopCartItems)) {
            return R.success(0);
        }
        Integer totalCount = shopCartItems.stream().map(ShopCartItemDto::getProdCount).reduce(0, Integer::sum);
        return R.success(totalCount);
    }
}
